<script src="/static/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="/static/layui/lib/layui-v2.5.5/css/layui.css" media="screen"/>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>增加</legend>
        </fieldset>

        <form class="layui-form" action="" id="number_form">
            <div class="layui-form-item">
                <label class="layui-form-label">匹配商家瑕疵图</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入商家名" class="layui-textarea" id="number" name="number"
                              lay-verify="required"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add_btn">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form'], function () {
        var form = layui.form
            , $ = layui.jquery ;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();


        //监听提交
        form.on('submit(add_btn)', function (data) {
            var formObject = {};
            var formArray = $("#number_form").serializeArray();
            $.each(formArray,function(i,item){
                formObject[item.name] = item.value;
            });
            $.ajax({
                type : 'GET',
                url : "/api/file?type=8&merchant_name="+formObject["number"],
                data : false,
                processData : false,
                contentType : false,
                success : function(result){
                    if (result.success == "false") {
                        alert("添加失败!");
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                    else if (result.exception == "true"){
                        alert("服务器异常");
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                    else if (result.success == "true"){
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
			    },error : function(result){
			            console.log(result);
                    }
		    });
        });

    });

</script>